﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Bank;
using Bank.Exceptions;
using Bank.Operations;
using System.Threading;

namespace BankUnitTest
{
    [TestClass]
    public class TestBankier
    {
        [TestMethod]
        public void CreatingBankier()
        {
            Banker banker = new Banker("Jan" , "Kowalski");
            Assert.AreEqual("Jan", banker.FirstName);
            Assert.AreEqual("Kowalski", banker.LastName);
        }

        [TestMethod]
        public void CreatingAddTwoSameClientWithSamePesel()
        {
            Banker banker = new Banker("Jan", "Kowalski");
            banker.addClient("Tomek", "Nowak", "1");
            banker.addClient("Alicja", "Janosz", "2");
            // add the client wich pesel exsist in database
            // this should throw exception
            banker.addClient("Tomek", "Nowak", "1");
        }

        [TestMethod]
        public void AddNewClient()
        {
            Banker banker = new Banker("Jan", "Kowalski");
            banker.addClient("Tomek", "Nowak", "1");
            Client newClient = banker.getClient("1");
            Assert.AreEqual("Tomek", newClient.FirstName);
            Assert.AreEqual("Kowalski", newClient.LastName);
        }

        [TestMethod]
        public void BlockClient()
        {
            Banker banker = new Banker("Jan", "Kowalski");
            banker.addClient("Tomek", "Nowak", "1");
            Client newClient = banker.getClient("1");
            newClient.Blocked = true;
            newClient.requestNewAccount(AccountType.CurrantAccount, newClient.ClientID, 0);
        }

        [TestMethod]
        public void UnBlockClient()
        {
            Banker banker = new Banker("Jan", "Kowalski");
            banker.addClient("Tomek", "Nowak", "1");
            Client newClient = banker.getClient("1");
            newClient.Blocked = true;
            
            try
            {
                newClient.requestNewAccount(AccountType.CurrantAccount, newClient.ClientID, 0);
            }
            catch (BlockedAccountException)
            {
                Console.WriteLine("Catched exceptions");
            }

            newClient.Blocked = false;
            newClient.requestNewAccount(AccountType.CurrantAccount, newClient.ClientID, 0);
            

        }

        [TestMethod]
        public void ListAllClient()
        {
            Banker banker = new Banker("Jan", "Kowalski");
            banker.addClient("Tomek", "Nowak", "1");
            banker.addClient("Alicja", "Janosz", "2");
            banker.addClient("Tomek", "Groch", "3");
            banker.addClient("Wojciech", "Nitka", "4");
            banker.listAllClients();
        }

        [TestMethod]
        public void CalculateInterest()
        {
            Banker banker = new Banker("Jan", "Kowalski");
            banker.addClient("Tomek", "Nowak", "1");

            Client client = banker.getClient("1");
            client.requestNewAccount(AccountType.CurrantAccount,client.ClientID,0);

            client.makePayment(1000, client.Accounts.First());
            client.makePayment(1000, client.Accounts.First());
            client.makePayment(1000, client.Accounts.First());

            banker.calculateInterests(client.ClientID, client.Accounts.First());
        
        }

        [TestMethod]
        public void CalculateInterest2()
        {
            Banker banker = new Banker("Jan", "Kowalski");
            banker.addClient("Tomek", "Nowak", "1");

            Client client = banker.getClient("1");
            client.requestNewAccount(AccountType.CurrantAccount, client.ClientID, new Money());            

            client.makePayment(new Money(1000), client.Accounts.First());
            client.makePayment(new Money(1000), client.Accounts.First());
            client.makePayment(new Money(1000), client.Accounts.First());

            banker.calculateInterests(client.ClientID, client.Accounts.First());

            

        }


        [TestMethod]
        public void ChangeCalculateInterest()
        {
            Banker banker = new Banker("Jan", "Kowalski");
            banker.addClient("Tomek", "Nowak", "1");

            Client client = banker.getClient("1");

            IAccount firstAccount = client.requestNewAccount(AccountType.CurrantAccount, client.ClientID, 0);
            IAccount secondAccount = client.requestNewAccount(AccountType.CurrantAccount, client.ClientID, 0);

            client.makePayment(1000, firstAccount.Number);
            client.makePayment(1000, firstAccount.Number);
            client.makePayment(1000, firstAccount.Number);

            client.makePayment(1000, secondAccount.Number);
            client.makePayment(1000, secondAccount.Number);
            client.makePayment(1000, secondAccount.Number);

            Console.WriteLine("First Account Balance = " + firstAccount.Ballance);
            Console.WriteLine("Second Account Balance = " + secondAccount.Ballance);

            client.Manager.changeInterestCalculator(client.ClientID, secondAccount.Number, new SimpleInterestCalculator());

            banker.calculateInterests(client.ClientID, firstAccount.Number);
            banker.calculateInterests(client.ClientID, secondAccount.Number);

            Console.WriteLine("First Account Balance = " + firstAccount.Ballance);
            Console.WriteLine("Second Account Balance = " + secondAccount.Ballance);

        }

        [TestMethod]
        public void MakeOperationOnAccount()
        {
            Banker banker = new Banker("Jan", "Kowalski");
            banker.addClient("Tomek", "Nowak", "1");

            Client client = banker.getClient("1");

            IAccount firstAccount = client.requestNewAccount(AccountType.CurrantAccount, client.ClientID, 0);
            
            client.makePayment(1000, firstAccount.Number);
            client.makePayment(1000, firstAccount.Number);
            client.makePayment(1000, firstAccount.Number);

            client.Manager.makeOperation(client.ClientID, firstAccount.Number, new InPayment(10));
           
            
            Console.WriteLine("First Account Balance = " + firstAccount.Ballance);
           

        }


        [TestMethod]
        public void MakeMonthlyReport()
        {
            Banker banker = new Banker("Jan", "Kowalski");
            banker.addClient("Tomek", "Nowak", "1");
            banker.addClient("Kasia", "Ziemkiewicz", "2");

            Client client1 = banker.getClient("1");
            Client client2 = banker.getClient("2");

            IAccount firstAccount = client1.requestNewAccount(AccountType.CurrantAccount, client1.ClientID, 0);
            IAccount secondAccount = client2.requestNewAccount(AccountType.CurrantAccount, client2.ClientID, 0);

            int step = 0;
            while (step < 12)
            {

                client1.Manager.makeOperation(client1.ClientID, firstAccount.Number, new InPayment(1000));
                client1.Manager.makeOperation(client1.ClientID, firstAccount.Number, new InPayment(2000));
                client1.Manager.makeOperation(client1.ClientID, firstAccount.Number, new InPayment(310));
                banker.calculateInterests(client1.ClientID, firstAccount.Number);

                client2.Manager.makeOperation(client2.ClientID, secondAccount.Number, new InPayment(2000));
                client2.Manager.makeOperation(client2.ClientID, secondAccount.Number, new InPayment(5000));
                client2.Manager.makeOperation(client2.ClientID, secondAccount.Number, new InPayment(410));
                banker.calculateInterests(client2.ClientID, secondAccount.Number);

                List<Client> clients = ClientCollections.ClientsList;
                
                
                foreach (Client c in clients)
                {
                    c.Manager.DoMonthlyReport();
                }
                
                Thread.Sleep(1);
                step++;

            }


        }



        [TestMethod]
        public void MakeMonthlyReport2()
        {
            Banker banker = new Banker("Jan", "Kowalski");
            banker.addClient("Tomek", "Nowak", "1");
            banker.addClient("Kasia", "Ziemkiewicz", "2");

            Client client1 = banker.getClient("1");
            Client client2 = banker.getClient("2");

            IAccount firstAccount = client1.requestNewAccount(AccountType.CurrantAccount, client1.ClientID, 0);
            IAccount secondAccount = client2.requestNewAccount(AccountType.CurrantAccount, client2.ClientID, new Money());

            int step = 0;
            while (step < 12)
            {

                client1.Manager.makeOperation(client1.ClientID, firstAccount.Number, new InPayment(1000));
                client1.Manager.makeOperation(client1.ClientID, firstAccount.Number, new InPayment(2000));
                client1.Manager.makeOperation(client1.ClientID, firstAccount.Number, new InPayment(310));
                banker.calculateInterests(client1.ClientID, firstAccount.Number);

                client2.Manager.makeOperation(client2.ClientID, secondAccount.Number, new InPayment(new Money(1000)));
                client2.Manager.makeOperation(client2.ClientID, secondAccount.Number, new InPayment(new Money(2000)));
                client2.Manager.makeOperation(client2.ClientID, secondAccount.Number, new InPayment(new Money(310)));
                banker.calculateInterests(client2.ClientID, secondAccount.Number);

                List<Client> clients = ClientCollections.ClientsList;

                client1.Manager.DoMonthlyReport();
                client2.Manager.DoMonthlyReport2();

                Thread.Sleep(1);
                step++;

            }


        }


    }
}
